package util

import "log"
import "fmt"
import "os"

type Logger struct {
	Logger *log.Logger
    logname string
	log_stdout bool
}

func NewLogger(name string, to_stdout bool) *Logger {
	kv := new(Logger)
	kv.log_stdout = to_stdout

	var fout *os.File
	if !kv.log_stdout {
		kv.logname = name + ".log"
		var err error
		fout, err = os.Create(kv.logname)
		if err != nil {
			fmt.Printf("Failed to create log file %v: %v\n", kv.logname, err)
			kv.log_stdout = true
		}
	}

	if !kv.log_stdout {
		kv.Logger = log.New(fout, "", 0)
	} else {
		kv.Logger = log.New(os.Stdout, "", 0)
	}
	
	return kv
}

func (kv *Logger) Remove() {
	os.Remove(kv.logname)
}

